home *** CD-ROM | disk | FTP | other *** search
/ Transformers: Revenge of …he Game: Press Kit (USA) / Transformers - Revenge of the Fallen - The Game - Press Kit (USA).bin / Transformers.swf / scripts / fl / video / SMILManager.as < prev    next >
Text File  |  2009-06-19  |  11KB  |  292 lines

  1. package fl.video
  2. {
  3.    import flash.events.Event;
  4.    import flash.events.IOErrorEvent;
  5.    import flash.events.SecurityErrorEvent;
  6.    import flash.net.URLLoader;
  7.    import flash.net.URLRequest;
  8.    
  9.    use namespace flvplayback_internal;
  10.    
  11.    public class SMILManager
  12.    {
  13.       
  14.       public static const VERSION:String = "2.1.0.19";
  15.       
  16.       public static const SHORT_VERSION:String = "2.1";
  17.        
  18.       
  19.       flvplayback_internal var width:int;
  20.       
  21.       flvplayback_internal var xmlLoader:URLLoader;
  22.       
  23.       flvplayback_internal var xml:XML;
  24.       
  25.       flvplayback_internal var height:int;
  26.       
  27.       private var _url:String;
  28.       
  29.       flvplayback_internal var videoTags:Array;
  30.       
  31.       flvplayback_internal var baseURLAttr:Array;
  32.       
  33.       private var _owner:INCManager;
  34.       
  35.       public function SMILManager(param1:INCManager)
  36.       {
  37.          super();
  38.          _owner = param1;
  39.          width = -1;
  40.          height = -1;
  41.       }
  42.       
  43.       flvplayback_internal function parseSwitch(param1:XML) : void
  44.       {
  45.          var _loc2_:* = null;
  46.          var _loc3_:XML = null;
  47.          default xml namespace = flvplayback_internal::xml.namespace();
  48.          for(_loc2_ in param1.*)
  49.          {
  50.             _loc3_ = param1.*[_loc2_];
  51.             if(_loc3_.nodeKind() != "element")
  52.             {
  53.                continue;
  54.             }
  55.             switch(_loc3_.localName())
  56.             {
  57.                case "video":
  58.                case "ref":
  59.                   flvplayback_internal::videoTags.push(parseVideo(_loc3_));
  60.                   break;
  61.             }
  62.          }
  63.       }
  64.       
  65.       flvplayback_internal function parseVideo(param1:XML) : Object
  66.       {
  67.          default xml namespace = flvplayback_internal::xml.namespace();
  68.          var _loc2_:Object = new Object();
  69.          if(param1.@src.length() > 0)
  70.          {
  71.             _loc2_.src = param1.@src.toString();
  72.          }
  73.          if(param1["system-bitrate"].length() > 0)
  74.          {
  75.             _loc2_.bitrate = int(param1["system-bitrate"].toString());
  76.          }
  77.          if(param1.@dur.length() > 0)
  78.          {
  79.             _loc2_.dur = parseTime(param1.@dur.toString());
  80.          }
  81.          return _loc2_;
  82.       }
  83.       
  84.       flvplayback_internal function connectXML(param1:String) : Boolean
  85.       {
  86.          _url = fixURL(param1);
  87.          xmlLoader = new URLLoader();
  88.          flvplayback_internal::xmlLoader.addEventListener(Event.COMPLETE,flvplayback_internal::xmlLoadEventHandler);
  89.          flvplayback_internal::xmlLoader.addEventListener(IOErrorEvent.IO_ERROR,flvplayback_internal::xmlLoadEventHandler);
  90.          flvplayback_internal::xmlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,flvplayback_internal::xmlLoadEventHandler);
  91.          flvplayback_internal::xmlLoader.load(new URLRequest(_url));
  92.          return false;
  93.       }
  94.       
  95.       flvplayback_internal function fixURL(param1:String) : String
  96.       {
  97.          var _loc2_:String = null;
  98.          if(/^(http:|https:)/i.test(param1))
  99.          {
  100.             _loc2_ = param1.indexOf("?") >= 0 ? "&" : "?";
  101.             return param1 + _loc2_ + "FLVPlaybackVersion=" + SHORT_VERSION;
  102.          }
  103.          return param1;
  104.       }
  105.       
  106.       flvplayback_internal function xmlLoadEventHandler(param1:Event) : void
  107.       {
  108.          var e:Event = param1;
  109.          try
  110.          {
  111.             if(e.type != Event.COMPLETE)
  112.             {
  113.                _owner.helperDone(this,false);
  114.             }
  115.             else
  116.             {
  117.                baseURLAttr = new Array();
  118.                videoTags = new Array();
  119.                xml = new XML(flvplayback_internal::xmlLoader.data);
  120.                default xml namespace = flvplayback_internal::xml.namespace();
  121.                if(flvplayback_internal::xml == null || flvplayback_internal::xml.localName() == null)
  122.                {
  123.                   throw new VideoError(VideoError.INVALID_XML,"URL: \"" + _url + "\" No root node found; if url is for an flv it must have .flv extension and take no parameters");
  124.                }
  125.                if(flvplayback_internal::xml.localName() != "smil")
  126.                {
  127.                   throw new VideoError(VideoError.INVALID_XML,"URL: \"" + _url + "\" Root node not smil");
  128.                }
  129.                checkForIllegalNodes(flvplayback_internal::xml,"element",["head","body"]);
  130.                if(flvplayback_internal::xml.head.length() > 0)
  131.                {
  132.                   parseHead(flvplayback_internal::xml.head[0]);
  133.                }
  134.                if(flvplayback_internal::xml.body.length() < 1)
  135.                {
  136.                   throw new VideoError(VideoError.INVALID_XML,"URL: \"" + _url + "\" Tag body is required.");
  137.                }
  138.                parseBody(flvplayback_internal::xml.body[0]);
  139.                _owner.helperDone(this,true);
  140.             }
  141.          }
  142.          catch(err:Error)
  143.          {
  144.             _owner.helperDone(this,false);
  145.             throw err;
  146.          }
  147.          finally
  148.          {
  149.             flvplayback_internal::xmlLoader.removeEventListener(Event.COMPLETE,flvplayback_internal::xmlLoadEventHandler);
  150.             flvplayback_internal::xmlLoader.removeEventListener(IOErrorEvent.IO_ERROR,flvplayback_internal::xmlLoadEventHandler);
  151.             flvplayback_internal::xmlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,flvplayback_internal::xmlLoadEventHandler);
  152.             xmlLoader = null;
  153.          }
  154.       }
  155.       
  156.       flvplayback_internal function checkForIllegalNodes(param1:XML, param2:String, param3:Array) : void
  157.       {
  158.          var _loc4_:* = null;
  159.          var _loc5_:Boolean = false;
  160.          var _loc6_:XML = null;
  161.          var _loc7_:String = null;
  162.          var _loc8_:* = null;
  163.          default xml namespace = flvplayback_internal::xml.namespace();
  164.          var _loc9_:int = 0;
  165.          var _loc10_:* = param1.*;
  166.          while(true)
  167.          {
  168.             for(_loc4_ in _loc10_)
  169.             {
  170.                _loc5_ = false;
  171.                if((_loc6_ = param1.*[_loc4_]).nodeKind() == param2)
  172.                {
  173.                   _loc7_ = _loc6_.localName();
  174.                   for(_loc8_ in param3)
  175.                   {
  176.                      if(param3[_loc8_] == _loc7_)
  177.                      {
  178.                         _loc5_ = true;
  179.                         break;
  180.                      }
  181.                   }
  182.                   if(!_loc5_)
  183.                   {
  184.                      break;
  185.                   }
  186.                }
  187.             }
  188.             return;
  189.          }
  190.          throw new VideoError(VideoError.INVALID_XML,"URL: \"" + _url + "\" " + param2 + " " + _loc7_ + " not supported in " + param1.localName() + " tag.");
  191.       }
  192.       
  193.       flvplayback_internal function parseHead(param1:XML) : void
  194.       {
  195.          default xml namespace = flvplayback_internal::xml.namespace();
  196.          checkForIllegalNodes(param1,"element",["meta","layout"]);
  197.          if(param1.meta.length() > 0)
  198.          {
  199.             checkForIllegalNodes(param1.meta[0],"element",[]);
  200.             checkForIllegalNodes(param1.meta[0],"attribute",["base"]);
  201.             if(param1.meta.@base.length() > 0)
  202.             {
  203.                flvplayback_internal::baseURLAttr.push(param1.meta.@base.toString());
  204.             }
  205.          }
  206.          if(param1.layout.length() > 0)
  207.          {
  208.             parseLayout(param1.layout[0]);
  209.          }
  210.       }
  211.       
  212.       flvplayback_internal function parseBody(param1:XML) : void
  213.       {
  214.          var _loc2_:XML = null;
  215.          var _loc4_:Object = null;
  216.          default xml namespace = flvplayback_internal::xml.namespace();
  217.          if(param1.*.length() != 1 || param1.*[0].nodeKind() != "element")
  218.          {
  219.             throw new VideoError(VideoError.INVALID_XML,"URL: \"" + _url + "\" Tag " + param1.localName() + " is required to contain exactly one tag.");
  220.          }
  221.          _loc2_ = param1.*[0];
  222.          var _loc3_:String = _loc2_.localName();
  223.          switch(_loc3_)
  224.          {
  225.             case "switch":
  226.                parseSwitch(_loc2_);
  227.                break;
  228.             case "video":
  229.             case "ref":
  230.                _loc4_ = parseVideo(_loc2_);
  231.                flvplayback_internal::videoTags.push(_loc4_);
  232.                break;
  233.             default:
  234.                throw new VideoError(VideoError.INVALID_XML,"URL: \"" + _url + "\" Tag " + _loc3_ + " not supported in " + param1.localName() + " tag.");
  235.          }
  236.          if(flvplayback_internal::videoTags.length < 1)
  237.          {
  238.             throw new VideoError(VideoError.INVALID_XML,"URL: \"" + _url + "\" At least one video of ref tag is required.");
  239.          }
  240.       }
  241.       
  242.       flvplayback_internal function parseTime(param1:String) : Number
  243.       {
  244.          var _loc3_:Number = NaN;
  245.          var _loc4_:Number = NaN;
  246.          default xml namespace = flvplayback_internal::xml.namespace();
  247.          var _loc2_:Object = /^((\d+):)?(\d+):((\d+)(.\d+)?)$/.exec(param1);
  248.          if(_loc2_ == null)
  249.          {
  250.             _loc3_ = Number(param1);
  251.             if(isNaN(_loc3_) || _loc3_ < 0)
  252.             {
  253.                throw new VideoError(VideoError.INVALID_XML,"Invalid dur value: " + param1);
  254.             }
  255.             return _loc3_;
  256.          }
  257.          return Number((_loc4_ = (_loc4_ = (_loc4_ = 0) + uint(_loc2_[2]) * 60 * 60) + uint(_loc2_[3]) * 60) + Number(_loc2_[4]));
  258.       }
  259.       
  260.       flvplayback_internal function parseLayout(param1:XML) : void
  261.       {
  262.          var _loc2_:XML = null;
  263.          var _loc3_:Number = NaN;
  264.          var _loc4_:Number = NaN;
  265.          default xml namespace = flvplayback_internal::xml.namespace();
  266.          checkForIllegalNodes(param1,"element",["root-layout"]);
  267.          if(param1["root-layout"].length() > 1)
  268.          {
  269.             throw new VideoError(VideoError.INVALID_XML,"URL: \"" + _url + "\" Only one base attribute supported in meta tag.");
  270.          }
  271.          if(param1["root-layout"].length() > 0)
  272.          {
  273.             _loc2_ = param1["root-layout"][0];
  274.             if(_loc2_.@width.length() > 0)
  275.             {
  276.                _loc3_ = Number(_loc2_.@width[0]);
  277.             }
  278.             if(_loc2_.@height.length() > 0)
  279.             {
  280.                _loc4_ = Number(_loc2_.@height[0]);
  281.             }
  282.             if(isNaN(_loc3_) || _loc3_ < 0 || isNaN(_loc4_) || _loc4_ < 0)
  283.             {
  284.                throw new VideoError(VideoError.INVALID_XML,"URL: \"" + _url + "\" Tag " + param1.localName() + " requires attributes width and height.  Width and height must be numbers greater than or equal to 0.");
  285.             }
  286.             width = Math.round(_loc3_);
  287.             height = Math.round(_loc4_);
  288.          }
  289.       }
  290.    }
  291. }
  292.